package com.example.asproject1801_1.async02;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import com.example.asproject1801_1.R;
import com.example.asproject1801_1.async01.VideoBean;
import com.example.asproject1801_1.utils.LoadNetDataAsyncTask;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JzvdStd;

public class ShowVideoActivity  extends AppCompatActivity implements LoadNetDataAsyncTask.onGetNetDataListener{
    String url = "http://baobab.kaiyanapp.com/api/v4/tabs/selected?udid=11111&vc=168&vn=3.3.1&deviceModel=Huawei6&first_channel=eyepetizer_baidu_market&last_channel=eyepetizer_baidu_market&system_version_code=20";
    ListView videoLv;
    List<VideoBean.ItemListBean> mDatas;
    private ShowVideoAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        mDatas = new ArrayList<>();
        videoLv = findViewById(R.id.video_lv);
        adapter = new ShowVideoAdapter(this, mDatas);
        videoLv.setAdapter(adapter);
//        执行异步任务
        LoadNetDataAsyncTask task = new LoadNetDataAsyncTask(this,this);
        task.execute(url);
    }

    private void parseData(String s) {
        //                解析数据
        VideoBean videoBean = new Gson().fromJson(s, VideoBean.class);
        List<VideoBean.ItemListBean> list = videoBean.getItemList();
//                遍历得到的集合，将想要显示的添加到原来的数据源中
        for (int i = 0; i < list.size(); i++) {
            VideoBean.ItemListBean itemBean = list.get(i);
            String type = itemBean.getType();
            if (type.equals("video")) {
                mDatas.add(itemBean);
            }
        }
        //把获取解析到的数据，添加到原来的数据源中
//                数据源发生变化了，提示适配器更新
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showDataFromNet(String s) {
        parseData(s);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JzvdStd.releaseAllVideos();  //释放正在播放的视频
    }
}